//
//  SSHomeViewController.swift
//  美团
//
//  Created by 叶竞泽 on 2017/5/14.
//  Copyright © 2017年 叶竞泽. All rights reserved.
//

import UIKit

class SSHomeViewController: UIViewController {
    
    // MARK: - 属性
    
    /// 顶部视图
    var topView: SSTopView?
    /// 分类控制器
    var catagoryViewController: SSCatagoryViewController?

    // MARK: - 生命周期
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupNavBar()
        setupTopView()
    }
}

// MARK: - 设置界面
extension SSHomeViewController {
    
    /// 设置导航栏
    fileprivate func setupNavBar() {
        let logoItem = UIBarButtonItem(image: UIImage(named: "icon_meituan_logo"), style: .done, target: nil, action: nil)
        logoItem.isEnabled = false
        navigationItem.leftBarButtonItems = [logoItem]
        navigationItem.leftBarButtonItem?.isEnabled = false
    }
    
    /// 设置顶部视图
    fileprivate func setupTopView() {
        topView = SSTopView.topView()
        topView?.delegate = self
        guard let topView = topView else {
            return
        }
        navigationItem.leftBarButtonItems?.append(UIBarButtonItem(customView: topView))
    }
}

// MARK: - 实现顶部视图代理
extension SSHomeViewController: SSTopViewDelegate {
    func topViewButtonClickWithCatagoryButton(sender: UIButton) {
        catagoryViewController = SSCatagoryViewController()
        catagoryViewController?.delegate = self
        catagoryViewController?.preferredContentSize = CGSize(width: 350, height: 300)
        catagoryViewController?.modalPresentationStyle = .popover
        catagoryViewController?.modalTransitionStyle = .coverVertical
        let style = catagoryViewController?.popoverPresentationController
        style?.barButtonItem = UIBarButtonItem(customView: sender)
        present(catagoryViewController!, animated: true) {
        }
    }
    func topViewButtonClickWithAreaButton(sender: UIButton) {
        print(#function)
    }
    func topViewButtonClickWithSortButton(sender: UIButton) {
        print(#function)
    }
}

// MARK: - 实现分类控制器点击回调
extension SSHomeViewController: SSCatagoryViewControllerDelegate {
    func didSeleted(withTitle: String, iconName: String, hlIconName: String, subName: String?) {
        // TODO: 根据拿到的信息设置topView的内容
        topView?.setCatagoryButton(title: withTitle, icon: iconName, hlIcon: hlIconName, subTtile: subName)
    }
}
